#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>

int main() {
    //define port and address
    short dest_port = 8080;
    unsigned char *dest_ip = "192.168.59.1";



    //1. create socket
    int socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (socketfd < 0) {
        perror("Error");
        exit(-1);
    }


    //2. create destination address structure
    struct sockaddr_in sockaddr_in_dest;
    bzero(&sockaddr_in_dest, sizeof(sockaddr_in_dest));
    sockaddr_in_dest.sin_family = AF_INET;
    sockaddr_in_dest.sin_port = htons(dest_port);
    inet_pton(AF_INET, dest_ip, &(sockaddr_in_dest.sin_addr));

    while (1){
        //3. send data
        //define data to be sent
        char send_buf[512] = "";
        fgets(send_buf, sizeof(send_buf), stdin);
        send_buf[strlen(send_buf)-1] = '\n';
        if(strlen(send_buf) == 2 && strcmp(send_buf[0], "-") == 0){
            close(socketfd);
            exit(-1);
        }

        sendto(socketfd, send_buf, strlen(send_buf), 0, (struct sockaddr *)&sockaddr_in_dest, sizeof(sockaddr_in_dest));
//        printf("%c", send_buf[0]);
    }



}

